from datetime import datetime
from typing import List
from vnpy.trader.object import TickData, OrderData, TradeData, BarData
from vnpy_ctastrategy.strategies import MyObj as mo

from vnpy_ctastrategy import (
    CtaTemplate
)
from time import time
from vnpy_ctastrategy.base import StopOrder

from vnpy_ctastrategy.strategies.myDb import MyTickData,saveMyTickData

from vnpy.trader.database import get_database

class TrackTest(CtaTemplate):
    """"""
    author = "Max"

    def __init__(self, cta_engine, strategy_name, vt_symbol, setting):

        self.old_turnover:float =0
        self.old_volume:float=0
        self.old_open_interest:float=0
        self.old_ask_price:float=0
        self.old_bid_price:float=0

        super().__init__(cta_engine, strategy_name, vt_symbol, setting)

    def on_init(self):
        """
        Callback when strategy is inited.
        """
        self.write_log("策略初始化")

    def on_start(self):
        """
        Callback when strategy is started.
        """
        self.write_log("策略启动")

    def on_stop(self):
        """
        Callback when strategy is stopped.
        """
        self.write_log("策略停止")

    def on_tick(self, tick: TickData):
        # print(tick.datetime)
        volume=tick.volume-self.old_volume
        if volume!=0 and self.old_volume!=0:
            # 根据价格发生位置计算主动买，卖量
            ask_price_volume=(tick.turnover-self.old_turnover)/10-volume*self.old_bid_price
            # 市价大单会产生超过 volume 的 ask_price_volume 说明扫了不止一档卖价 这里都算作卖一价，同理相对的都算作买一价
            if ask_price_volume>volume:ask_price_volume=volume
            if ask_price_volume<0:ask_price_volume=0
            bid_price_volume=volume-ask_price_volume
            mo.newTrack(self.old_ask_price)
            mo.newTrack(self.old_bid_price)
            track_ask_price=mo.setTrack(self.old_ask_price,bid_volume=ask_price_volume,tick=tick)
            track_bid_price=mo.setTrack(self.old_bid_price,ask_volume=bid_price_volume,tick=tick)

            # duokai=0
            # kongkai=0
            # duoping=0
            # kongping=0
            # b_duokai=0
            # b_kongkai=0
            # b_duoping=0
            # b_kongping=0
            # 开平判断
            cang_volume=tick.open_interest-self.old_open_interest
            # 开仓
            if cang_volume>0:
                # 多开
                if ask_price_volume>bid_price_volume:
                    pass
                else:
                    pass
            # 平仓 / 换
            else:
                # 主动
                track_ask_price.kongping=ask_price_volume
                track_bid_price.duoping=bid_price_volume
                # 被动
                if kongping>duoping:

                    



            # open_volume=volume+open_interest_volume
            # close_volume=volume-open_interest_volume
            
            # b_duokai=0
            # b_kongkai=0
            # b_duoping=0
            # b_kongping=0
            # 累计主动量 1
            # # newTrack(self.old_ask_price)
            # # newTrack(self.old_bid_price)

            # 开仓多
            # if open_volume>=close_volume:
            #     # 多开多
                # if ask_price_volume>=bid_price_volume:
            #         # 主动
            #         duokai=ask_price_volume
            #         kongkai=open_volume-duokai
            #         # 被动 后续
            #     else:
            #         # 主动
            #         kongkai=bid_price_volume
            #         duokai=open_volume-kongkai
            #     # 累计主动量 2
            #     mo.setTrack(self.old_ask_price,bid_volume=duokai)
            #     mo.setTrack(self.old_bid_price,ask_volume=kongkai)
            # # 平仓多
            # else:
            #     # 空平多
            #     if ask_price_volume>=bid_price_volume:
            #         kongping=ask_price_volume
            #         duoping=close_volume-kongping
            #         # 被动 后续
            #     else:
            #         duoping=bid_price_volume
            #         kongping=close_volume-duoping
            #     # 累计主动量 3
            #     mo.setTrack(self.old_ask_price,bid_volume=kongping)
            #     mo.setTrack(self.old_bid_price,ask_volume=duoping)
            if tick.volume>=0:
                print(f"LastP={tick.last_price} vol={volume} i={cang_volume} bidV={bid_price_volume} askV={ask_price_volume} OBidP={self.old_bid_price} OAskP={self.old_ask_price} BidP={tick.bid_price_1} AskP={tick.ask_price_1}")
                # print(f"{'lastP':>6s}{'vol':>6s}{'intrs':>6s}{'openV':>6s}{'close':>6s}{'bidV':>6s}{'askV':>6s}{'duoK':>6s}{'kongK':>6s}{'duoP':>6s}{'kongP':>6s}{'LBP':>6s}{'LAP':>6s}{'bidP':>6s}{'askP':>6s}")
                # print(f"{tick.last_price:>6.0f}{volume:>6.0f}{0:>6.0f}{0:>6.0f}{0:>6.0f}{bid_price_volume:>6.0f}{ask_price_volume:>6.0f}{0:>6.0f}{0:>6.0f}{0:>6.0f}{0:>6.0f}{self.old_bid_price:>6.0f}{self.old_ask_price:>6.0f}{tick.bid_price_1:>6.0f}{tick.ask_price_1:>6.0f}")
                # mo.old_ask_price=tick.ask_price_1
                # mo.old_bid_price=tick.bid_price_1
                mo.showTrack(tick)
            # print(f"{tick.turnover},{tick.turnover-self.old_turnover},{tick.volume},{tick.volume-self.old_volume},{tick.open_interest},{tick.open_interest-self.old_open_interest},{tick.bid_price_1},{tick.ask_price_1},{tick.last_price},{duokai},{kongkai},{duoping},{kongping}")
            # 数据对比 
            # print(f"{tick.last_price},{volume},{open_interest_volume},{open_volume},{close_volume},{bid_price_volume},{ask_price_volume},{duokai},{kongkai},{duoping},{kongping},{tick.bid_price_1},{tick.ask_price_1}")
            
        self.old_turnover=tick.turnover
        self.old_volume=tick.volume
        self.old_open_interest=tick.open_interest
        self.old_ask_price=tick.ask_price_1
        self.old_bid_price=tick.bid_price_1

        
        mo.high_price=tick.high_price
        mo.low_price=tick.low_price

        if not mo.open_price:
            mo.open_price=tick.open_price
            mo.pre_close=tick.pre_close
        
    def on_bar(self, bar: BarData):
        """
        Callback of new bar data update.
        """
        pass

    def on_order(self, order: OrderData):
        """
        Callback of new order data update.
        """
        self.put_event()

    def on_trade(self, trade: TradeData):
        """
        Callback of new trade data update.
        """
        self.put_event()

    def on_stop_order(self, stop_order: StopOrder):
        """
        Callback of stop order update.
        """
        self.put_event()
        